package org.test.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * 相关于date的util
 * DdateUtil 
 *  
 * @author：denggm@asiainfo-linkage.com
 * @Nov 23, 2012 10:04:21 AM 
 *
 * Copyright asiainf-linkage Corporation 2012
 */
public class DdateUtil {
	
	static Logger log = Logger.getLogger(DdateUtil.class); 


	/**
	 * 查询月的天数
	 * @param dateString 形如 “2012-22”
	 * @param pattern 对应于dateString ,如上，则为"yyyy-MM"
	 * @return
	 * @throws ParseException 
	 * int
	 * @author：denggm@asiainfo-linkage.com
	 * @Nov 23, 2012 10:33:30 AM
	 */
	public static int getMonthDayNumByDate(String dateString ,String pattern) throws ParseException{
		SimpleDateFormat currentDateFormat = new SimpleDateFormat(pattern);
		Date pDate = currentDateFormat.parse(dateString);
		return getMonthDayNumByDate(pDate);
	}
	
	/**
	 * 查询月的天数
	 * @param pDate
	 * @return
	 * @throws ParseException 
	 * int
	 * @author：denggm@asiainfo-linkage.com
	 * @Nov 23, 2012 10:39:39 AM
	 */
	public static int getMonthDayNumByDate(Date pDate) throws ParseException{
		Calendar pCalendar = Calendar.getInstance();
		pCalendar.setTimeInMillis(pDate.getTime());
		int dayNum = pCalendar.getActualMaximum(Calendar.DAY_OF_MONTH);
		return dayNum;
	}
	
	/**
	 * 得到当月往前推的6个月份list,形如：[{"value":"2012-11","name":"2012-11"},{..}]
	 * @return 
	 * List
	 * @author：denggm@asiainfo-linkage.com
	 * @Dec 2, 2012 2:02:04 PM
	 */
	public static List getMonthList() {
		List typeList = new ArrayList();
		Calendar pCalendar = Calendar.getInstance();
		//生成月份list
		String dateString = DataFormat.cvtFormattedCalendar(pCalendar, "yyyy-MM");
		int i=0;
		do{
			log.debug(i+","+dateString);
			pCalendar.add(Calendar.MONTH, -1);
			dateString = DataFormat.cvtFormattedCalendar(pCalendar, "yyyy-MM");
			Map tmpData = new HashMap();
			tmpData.put("value", dateString);
			tmpData.put("name", dateString);
			typeList.add(tmpData);
			i++;
		}while(i<6);
		return typeList;
	}
	
	


}
